﻿///////////////////////////////////////////////////////////////////////////////
//
//  This source is part of the  "Managed Task Framework.NET Runtime"  (MTF.NET Runtime).
//
//
//  Copyright (c) 2011, Multicore Consulting Limited (UK)
//  All rights reserved.
//  http://www.multicoreconsulting.co.uk
//
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions
//  are met:
//
//  -  Redistributions of source code must retain the above copyright
//     notice, this list of conditions and the following disclaimer.
//  -  Redistributions in binary form must reproduce the above copyright
//     notice, this list of conditions and the following disclaimer in
//     the documentation and/or other materials provided with the
//     distribution.
//  -  Neither the name of Multicore Consulting Limited nor the names of
//     its contributors may be used to endorse or promote products
//     derived from this software without specific prior written
//     permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
//  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
//  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
//  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
//  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
//  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//  POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////////

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace MTFOrg.MTF.MTFRuntime
{
    /// <summary>
    /// Implements a prioritized queue with discrete priority levels
    /// </summary>
    class PriorityQueue<T> where T : class
    {
        /// <summary>
        /// Number of discrete priorities.
        /// </summary>
        private int m_NumPriorities;

        /// <summary>
        /// Number of items in the queue.
        /// </summary>
        private int m_Count;

        /// <summary>
        /// Array of queues (1 per priority).
        /// </summary>
        private Queue<T>[] m_Queues;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="numPriorities">Number of priorities</param>
        public PriorityQueue(int numPriorities)
        {
            m_NumPriorities = numPriorities;
            m_Queues = new Queue<T>[m_NumPriorities];
            for (int i = 0; i < m_NumPriorities; i++)
            {
                m_Queues[i] = new Queue<T>();
            }
        }

        /// <summary>
        /// Gets the number of items in the queue.
        /// </summary>
        public int Count
        {
            get { return m_Count; }
        }

        /// <summary>
        /// Enqueues an item at the specified priority.
        /// </summary>
        /// <param name="item">Item to enqueue</param>
        /// <param name="priority">Priority to use</param>
        public void Enqueue(T item, int priority)
        {
            m_Queues[priority].Enqueue(item);
            m_Count++;
        }

        /// <summary>
        /// Dequeues the highest priority item available.
        /// </summary>
        /// <returns></returns>
        public T Dequeue()
        {
            if (m_Count == 0) return null;

            T item = null;
            for (int i = 0; i < m_NumPriorities; i++)
            {
                if (m_Queues[m_NumPriorities - i - 1].Count > 0)
                {
                    item = m_Queues[m_NumPriorities - i - 1].Dequeue();
                    break;
                }
            }

            m_Count--;

            return item;
        }
    }
}